from fastapi import APIRouter, Body, Query

from app.api.deps import CurrentUserDependency, DbDependency
from app.crud import user_address_crud
from app.schemas.user_address import AddressBase
from app.utils.response import success_response

router = APIRouter()

@router.post("/edit", summary="编辑收货地址")
async def address_edit(db: DbDependency, user: CurrentUserDependency,
                       address: AddressBase):
    return success_response(
        user_address_crud.address_edit(db, user.id, address))


@router.get("/detail", summary="获取详情")
async def get_address(db: DbDependency,
                      user: CurrentUserDependency,
                      addressId: int = Query(..., description="购物车id")):
    return success_response(user_address_crud.get_address(db, addressId))


@router.post("/remove", summary="删除")
async def remove(db: DbDependency,
                 user: CurrentUserDependency,
                 addressId: int = Body(..., embed=True)):
    return success_response(user_address_crud.del_address(db, addressId))


@router.post("/setDefault", summary="设置默认值")
async def setDefault(db: DbDependency,
                     user: CurrentUserDependency,
                     addressId: int = Body(..., embed=True)):
    return success_response(user_address_crud.set_defult(db, addressId))


@router.post("/add", summary="添加收货地址")
async def add(db: DbDependency, user: CurrentUserDependency,
              address: AddressBase):
    return success_response(
        user_address_crud.address_save(db, user.id, address))


@router.get("/defaultId", summary="默认收货地址id")
async def defaultId(db: DbDependency, user: CurrentUserDependency):
    return success_response(user_address_crud.get_defult(db, user.id))


@router.get("/list", summary="购物车列表")
async def address_list(db: DbDependency, user: CurrentUserDependency):
    return success_response(user_address_crud.address_list(db, user.id))
